package com.lzb.yabbs.net;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.lzb.yabbs.dao.Card;
import com.lzb.yabbs.interfaces.GetCardsCallback;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuzaibing on 2015/9/7.
 */
public class GetCardsFromNet {
    public static void getCards(String url, final GetCardsCallback getCardsCallback,List<BasicNameValuePair> pairs){
        OkHttpClientRequest.requestGet(url, pairs, new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                String resultStr=response.body().string();
                if(resultStr==null) {
                    getCardsCallback.getCardsSuccess(null);
                    return;
                }
                Log.d(getClass().getSimpleName(),resultStr);
                List<Card> cards;
                cards= JSONObject.parseArray(resultStr,Card.class);
                if(cards==null){
                    getCardsCallback.getCardsSuccess(null);
                    return;
                }

                getCardsCallback.getCardsSuccess(cards);
            }
        });
    }
}
